
/*
离线订单 1.0
 */
import 'dart:io';

import 'package:reabampos/src/reabam/operation/bean/h5_search_good.dart';
import 'package:reabampos/src/reabam/operation/bean/h5_upload_order.dart';
import 'package:reabampos/src/reabam/operation/bean/local_1/h5_bean_offline_order.dart';
import 'package:reabampos/src/reabam/operation/db/models.dart';
import 'package:reabampos/src/reabam/operation/db/objectbox.dart';
import 'package:reabampos/src/reabam/operation/rb_config.dart';
import 'package:reabampos/src/reabam/ui/pagestate_main_webview.dart';
import 'package:reabampos/src/reabam/utils/rb_business.dart';
import 'package:xsdk_flutter/xsdk/xsdk_flutter_package.dart';

class SupportOfflineOrder {

  MainWebViewStatePage statePage;

  SupportOfflineOrder(this.statePage);

  //////////////////////////////////////////////

  //保存离线订单 ok
  void saveOfflineOrder(dynamic data){

    H5BeanOfflineOrder h5beanOfflineOrder;
    if(data is String){
      h5beanOfflineOrder=H5BeanOfflineOrder.fromJson(XJsonUtils.json2Map(data));
    }else{
      h5beanOfflineOrder=H5BeanOfflineOrder.fromJson(data??{});
    }

    XLogUtils.printLog('---收到h5通知保存离线下单收银订单: orderNo=${h5beanOfflineOrder.orderNo}, data=$data');
    BeanOfflineDBOrder item=BeanOfflineDBOrder();
    item.orderId=h5beanOfflineOrder.orderId;
    item.orderNo=h5beanOfflineOrder.orderNo;
    item.orderDate=h5beanOfflineOrder.orderDate;
    item.json=XJsonUtils.obj2Json(h5beanOfflineOrder.json);
    XObjectBoxUtils.saveOfflineOrder(item);
  }
  //保存挂单(数据库)ok
  void savePendingOrder(dynamic data){

    H5BeanOfflinePendingOrder h5beanOfflinePendingOrder;
    if(data is String){
      h5beanOfflinePendingOrder=H5BeanOfflinePendingOrder.fromJson(XJsonUtils.json2Map(data));
    }else{
      h5beanOfflinePendingOrder=H5BeanOfflinePendingOrder.fromJson(data);
    }

    XLogUtils.printLog('---收到h5通知保存挂单');
    BeanOfflineDBPendingOrder item=BeanOfflineDBPendingOrder();
    item.creatDate=XDateUtils.getCurrentTime_String(format: XDateUtils.formatTimeDefault2);
    item.serialNumber=RBBusinessUtils.getSerNo();
    item.totalMoney='${h5beanOfflinePendingOrder.totalMoney}';
    item.json=XJsonUtils.obj2Json(h5beanOfflinePendingOrder.json);
    item.items=XJsonUtils.obj2Json(h5beanOfflinePendingOrder.items);

    XObjectBoxUtils.savePendingOrder(item);
  }
  //获得挂单订单列表(数据库) 旧
  void getOfflinePendingOrderList(dynamic data){

    BeanH5RequestPageGoods h5beanOfflinePendingOrder;
    if(data is String){
      h5beanOfflinePendingOrder=BeanH5RequestPageGoods.fromJson(XJsonUtils.json2Map(data));
    }else{
      h5beanOfflinePendingOrder=BeanH5RequestPageGoods.fromJson(data);
    }

    XObjectBoxUtils.getOfflinePendingOrderList(
        h5beanOfflinePendingOrder.pageIndex,
        h5beanOfflinePendingOrder.pageSize,
            (Map<String,dynamic> result){
          String json=XJsonUtils.obj2Json(result);
              statePage.runJSFunction(statePage.jsFunction_getOfflineDataCallback('offlinePendingOrderList',json));
        });
  }
  //获得挂单订单列表(数据库) 新
  void getPendingOrderListWithItems(dynamic data){

    BeanH5RequestPageGoods h5beanOfflinePendingOrder;
    if(data is String){
      h5beanOfflinePendingOrder=BeanH5RequestPageGoods.fromJson(XJsonUtils.json2Map(data));
    }else{
      h5beanOfflinePendingOrder=BeanH5RequestPageGoods.fromJson(data);
    }

    XObjectBoxUtils.getPendingOrderListWithItems(
        h5beanOfflinePendingOrder.pageIndex,
        h5beanOfflinePendingOrder.pageSize,
            (Map<String,dynamic> result){
              String json=XJsonUtils.obj2Json(result);

              XLogUtils.printLog('---getPendingOrderListWithItems,json=$json');

              statePage.runJSFunction(statePage.jsFunction_getAppValue('getPendingOrderListWithItems',json));
        });
  }
  void getPendingOrderDetail(dynamic data){

    Future((){
      int longId=int.parse('$data');
      BeanUploadOrderH5Response5 response=XObjectBoxUtils.getPendingOrderDetail(longId);
      String json=XJsonUtils.obj2Json(response);

      //特殊字符过滤
      json=json.replaceAll("<span style='color: #ff6600;'>", '');
      json=json.replaceAll('<\/span>', '');

      XLogUtils.printLog('---${json.contains("<span style='color: #ff6600;'>")}');
      XLogUtils.printLog('---${json.contains('<\/span>')}');


      //判断字符串长度
      int jsonLength=json.length;

      XLogUtils.printLog('---测试数据库结果jsonLength=$jsonLength,json=$json');

      //分割字符串长度算法
      int cutLength=1000000;
      //分割总数
      int totalLines=0;
      if(jsonLength>(cutLength+1)){
        //需要分割
        XLogUtils.printLog('---需要分割');

        int cutNum= (jsonLength/cutLength).truncate();
        totalLines=cutNum;

        //
        if(jsonLength%cutLength!=0){
          totalLines=cutNum+1;
        }
        //cutNum=2
        for(int i=0;i<cutNum;i++){
          String key1='$i';
          String json1=json.substring(i*cutLength,(i+1)*cutLength);

          XLogUtils.printLog('---key1=$key1, totalLines=$totalLines,json1=$json1');
          statePage.runJSFunction(_jsFunction_getOfflineDataCut_longJson('getPendingOrderDetailCut',json1,key1,'$totalLines'));
        }

        //
        if(jsonLength%cutLength!=0){
          String key1='$cutNum';
          String json1=json.substring(cutNum*cutLength);
          XLogUtils.printLog('---结尾 key1=$key1, totalLines=$totalLines,json1=$json1');
          statePage.runJSFunction(_jsFunction_getOfflineDataCut_longJson('getPendingOrderDetailCut',json1,key1,'$totalLines'));
        }

      }else{
        //不需要分割
        XLogUtils.printLog('---不需要分割');
        statePage.runJSFunction(statePage.jsFunction_getAppValue('getPendingOrderDetail',json));
      }

    });

  }

  String _jsFunction_getOfflineDataCut_longJson(String key,String json,String key1,String totalLines){
    return 'javascript:getOfflineDataCallback("$key","$json","$key1","$totalLines")';
  }


  //获得离线下单收银订单列表分页数据(异步处理)(数据库)ok
  void getOfflineOrderList(dynamic data){

    BeanH5RequestPageGoods h5beanOfflinePendingOrder;
    if(data is String){
      h5beanOfflinePendingOrder=BeanH5RequestPageGoods.fromJson(XJsonUtils.json2Map(data));
    }else{
      h5beanOfflinePendingOrder=BeanH5RequestPageGoods.fromJson(data);
    }

    XObjectBoxUtils.getOfflineOrderList(
        h5beanOfflinePendingOrder.pageIndex,
        h5beanOfflinePendingOrder.pageSize,
        h5beanOfflinePendingOrder.sword,
            (Map<String,dynamic> result){
              String json=XJsonUtils.obj2Json(result);
              statePage.runJSFunction(statePage.jsFunction_getOfflineDataCallback('offlineOrderItems',json));
        });
  }
  //删除离线下单收银订单(数据库) ok
  void delOfflineOrder(dynamic data){
    try{

      int id=int.tryParse('$data')??0;
      XObjectBoxUtils.delOfflineOrder(id);
    }catch(e){
      XLogUtils.printLog('$e');
    }
  }
  //删除挂单(数据库) ok
  void delPendingOrder(dynamic data){
    try{

      int id=int.tryParse('$data')??0;
      XObjectBoxUtils.delPendingOrder(id);
    }catch(e){
      XLogUtils.printLog('$e');
    }
  }
  //删除所有挂单
  void delAllPendingOrder(){
    XLogUtils.printLog('---收到h5通知删除所有挂单');
    try{
      XObjectBoxUtils.delAllPendingOrder();
    }catch(e){
      XLogUtils.printLog('$e');
    }
  }
  //获得挂单订单列表总数量(数据库)ok
  void getOfflinePendingOrderTotalCount(){
    XLogUtils.printLog('---收到h5通知获得挂单订单列表总数量');
    try{
      int count=XObjectBoxUtils.getPendingOrderTotalCount();
      statePage.runJSFunction(statePage.jsFunction_getOfflineDataCallback('offlinePendingOrderTotalCount','$count'));
    }catch(e){
      XLogUtils.printLog('$e');
    }
  }



}